package com.bit101.components {
	import com.bit101.components.Interface.ITextFormat;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;

	/**
	 * 文本输入框
	 * 扩展自基类及文本格式接口
	 * 
	 */
	public class TextInput extends Component implements ITextFormat{
		protected var _back:Sprite;
		protected var _password:Boolean = false;
		protected var _text:String = "";
		protected var _tf:TextField;
		protected var _format:TextFormat;
		protected var _defaultHandler:Function;

		/**
		 * Constructor
		 * @param parent 
		 * @param xpos 
		 * @param ypos 
		 * @param text 
		 * @param defaultHandler 侦听change事件
		 */
		public function TextInput(parent:DisplayObjectContainer = null, xpos:Number = 0, ypos:Number = 0, text:String = "", defaultHandler:Function = null){
			this.text = text;
			super(parent, xpos, ypos);
			
			if (defaultHandler != null){
				addEventListener(Event.CHANGE, defaultHandler);
			}
		}

		/**
		 * 初始化，执行addChildren
		 * 时间侦听draw函数
		 */
		override protected function init():void {
			super.init();
			setSize(100, 22);
		}

		/**
		 * 创建及添加子显示对象
		 */
		override protected function addChildren():void {
			_back = new Sprite();
			_back.filters = [getShadow(2, true)]; //基类定义滤镜方法
			addChild(_back);
			
			_tf = new TextField();
			_tf.embedFonts = Style.embedFonts;
			_tf.selectable = true;
			_tf.type = TextFieldType.INPUT;
			_tf.defaultTextFormat = new TextFormat(Style.fontName, Style.fontSize, Style.INPUT_TEXT);
			addChild(_tf);
			_tf.addEventListener(Event.CHANGE, onChange);
		}

		/**
		 * 绘制可视UI组件
		 */
		override public function draw():void {
			super.draw();
			_back.graphics.clear();
			_back.graphics.lineStyle(1,Style.LABEL_TEXT)
			_back.graphics.beginFill(Style.BACKGROUND);
			_back.graphics.drawRect(0, 0, _width, _height);
			_back.graphics.endFill();
			
			_tf.displayAsPassword = _password;
			if (_text != null){
				_tf.text = _text;
			} else {
				_tf.text = "";
			}
			//设置文本的大小
			_tf.width = _width - 4;
			if (_tf.text == ""){
				_tf.text = "X";
				_tf.height = Math.min(_tf.textHeight + 4, _height);
				_tf.text = "";
			} else {
				_tf.height = Math.min(_tf.textHeight + 4, _height);
			}
			//trace(_tf.width,_tf.textWidth,_tf.height,_tf.textHeight)
			/*_tf.background = true;
			_tf.backgroundColor = Style.BACKGROUND;
			_tf.border = true;
			_tf.borderColor = Style.INPUT_TEXT;*/
			_tf.x = 2;
			_tf.y = Math.round((_height- _tf.height) / 2);
		}
		
		public function setFontSize(size:uint, color:uint = 0x000000):void{
			if(_format==null){
				_format = new TextFormat(null,size,color);
			}else{
				_format.size = size;
				_format.color= color;
			}
			setTextFormat(_format);
		}
		public function setTextFormat(format:TextFormat):void{
			_tf.setTextFormat(format);
			_tf.defaultTextFormat = format;
			if(_height < format.size){
				_height = int(format.size) + 3;
			}
		}

		/**
		 * 文本框变化事件
		 * 阻止事件冒泡
		 * @param event The Event passed by the system.
		 */
		protected function onChange(event:Event):void {
			_text = _tf.text;
			event.stopImmediatePropagation(); //阻止事件冒泡
			this.dispatchEvent(event);
		}

		///////////////////////////////////
		// getter/setters
		///////////////////////////////////
		/**
		 * 设置文重新绘制UI
		 */
		public function set text(t:String):void {
			_text = t;
			if (_text == null)
				_text = "";
			invalidate();       //重新绘画UI
		}

		public function get text():String {
			return _text;
		}

		public function get textField():TextField {
			return _tf;
		}

		public function set restrict(str:String):void {
			_tf.restrict = str;
		}

		public function get restrict():String {
			return _tf.restrict;
		}

		public function set maxChars(max:int):void {
			_tf.maxChars = max;
		}

		public function get maxChars():int {
			return _tf.maxChars;
		}

		public function set password(b:Boolean):void {
			_password = b;
			invalidate();
		}

		public function get password():Boolean {
			return _password;
		}

		public override function set enabled(value:Boolean):void {
			super.enabled = value;
			_tf.tabEnabled = value;
		}

		public function set defaultHandler(value:Function):void {
			_defaultHandler = value;
			this.addEventListener(Event.CHANGE,value);
		}

	}
}